package com.example.sea.back.web.information;

import com.alibaba.druid.util.StringUtils;
import com.example.sea.back.common.context.SessionKeyContext;
import com.example.sea.core.data.information.entity.InformationNotice;
import com.example.sea.core.data.information.service.InformationNoticeService;
import com.example.sea.core.data.user.entity.AuthUser;
import com.example.sea.core.data.user.model.AuthUserModel;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 *
 * @author zengyz
 * @date 2018/7/9 上午 11:54
 */
@Controller
@RequestMapping("/information/notice")
public class InformationNoticeController {

    @Autowired
    private InformationNoticeService informationNoticeService;

    @RequiresAuthentication
    @GetMapping("/index")
    public String indexHtml(HttpServletRequest request, Integer pageNum, Integer pageSize){
        Map params=new HashMap();
        params.put("pageNum",pageNum==null?1:pageNum);
        params.put("pageSize",pageSize==null?10:pageSize);

        PageInfo<InformationNotice> pageInfo = informationNoticeService.findPageByParams(params);
        request.setAttribute("pageInfo",pageInfo);
        return "information/notice/index";
    }

    /**
     * 添加页面
     * @param request
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/add")
    public String addHtml(HttpServletRequest request){
        return "information/notice/add";
    }

    /**
     * 添加
     * @param informationNotice
     * @return
     */
    @RequiresAuthentication
    @ResponseBody
    @PostMapping("/add")
    public Map addSubmit(InformationNotice informationNotice){
        AuthUser authUser = (AuthUser) SecurityUtils.getSubject().getSession().getAttribute(SessionKeyContext.SESSION_LOGIN_ADMIN_USER);
        Map resultMap=new HashMap();
        informationNotice.setCreateUserId(authUser.getId());
        int add = informationNoticeService.add(informationNotice);
        if (add==1){
            resultMap.put("code",200);
        }else{
            resultMap.put("code",500);
        }
        return resultMap;
    }

    /**
     * 删除
     * @param request
     * @param chkids
     * @return
     */
    @RequiresAuthentication
    @ResponseBody
    @PostMapping("/delete")
    public Map deleteSubmit(HttpServletRequest request,String chkids){
        Map resultMap=new HashMap();
        String[] ids = chkids.split(",");
        int delete = informationNoticeService.delete(ids);
        if (delete!=ids.length){
            resultMap.put("code",500);
        }else{
            resultMap.put("code",200);
        }
        return resultMap;
    }

    /**
     * 修改页面
     * @param request
     * @param id
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/update")
    public String updateHtml(HttpServletRequest request,String id,String type){
        InformationNotice informationNotice = informationNoticeService.findById(id);
        request.setAttribute("informationNotice",informationNotice);

        request.setAttribute("isEdit",type);
        return "information/notice/edit";
    }
}
